package structs.quantity;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;

/**
 * Represents the location of a quantity of books.
 * @author SJP
 *
 */
public class Location implements Serializable, Comparable<Location>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 769795243076403729L;
	private String loc;
	
	private static HashMap<String,Location> locs = new HashMap<String,Location>();
	
	/**
	 * Creates a new location with the given location string.
	 * @param l the location
	 */
	private Location(String l) {
		loc = l;
	}
	
	/**
	 * @return the location
	 */
	@Override
	public String toString() {return loc;}
	
	/**
	 * Sets a new location
	 * @param l the new location
	 * @return the new location
	 */
	public String setLocation(String l) {return loc = l;}
	
	private void writeObject(ObjectOutputStream out) throws IOException {
		out.defaultWriteObject();
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
	}

	@Override
	public int compareTo(Location o) {
		return loc.compareTo(o.loc);
	}
	
	public static Location get(String l) {
		Location out = locs.get(l);
		if (out == null)
			out = locs.put(l, new Location(l));
		
		return out;
	}
	
}
